package com.store.mapper;

import com.store.entity.Category;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface CategoryMapper {
    @Select("SELECT * FROM category ORDER BY create_time DESC LIMIT #{offset}, #{pageSize}")
    List<Category> selectList(@Param("offset") int offset, @Param("pageSize") int pageSize);

    @Select("SELECT * FROM category WHERE category_id = #{categoryId}")
    Category selectById(Long categoryId);

    @Insert("INSERT INTO category (category_name, description, create_time, update_time) " +
            "VALUES (#{categoryName}, #{description}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "categoryId")
    int insert(Category category);

    @Update("UPDATE category SET category_name = #{categoryName}, description = #{description}, " +
            "update_time = #{updateTime} WHERE category_id = #{categoryId}")
    int update(Category category);

    @Delete("DELETE FROM category WHERE category_id = #{categoryId}")
    int deleteById(Long categoryId);

    @Select("SELECT COUNT(*) FROM category")
    long count();

    @Select("SELECT * FROM category WHERE category_name LIKE CONCAT('%', #{keyword}, '%') LIMIT #{offset}, #{pageSize}")
    List<Category> selectListByKeyword(@Param("keyword") String keyword, @Param("offset") Integer offset, @Param("pageSize") Integer pageSize);

    @Select("SELECT COUNT(*) FROM category WHERE category_name LIKE CONCAT('%', #{keyword}, '%')")
    long countByKeyword(String keyword);

    @Select("SELECT * FROM category")
    List<Category> selectAll();
} 